// implements the input phase, where the user responds to a given set of shapes.
// the response is saved in the 'data/<username>/qr' file.

public class PLPhaseInput {
	PLPlayer[] mPlayers = new PLPlayer[2];
	volatile boolean mExit = false;

	public PLPhaseInput() {}

	public void Play(String name) {
		if(!Init()) {
			PLDebug.Err("Could Not init game");
			return;
		}
			
		PlayGame(name);
		Shutdown();
	}

	public void ExitNow() {
		mExit = true;
	}	

	// PRIVATE 

	private boolean InitSketch() {
		PLSketch.Obj().clear();
		return true;
	}

	private boolean InitPlayers() {
		// the ordering is important here !
		mPlayers[0] = PLPlayer.GetPlayerFromName("BasicShapes");		
		if (mPlayers[0] == null)
			return false;

		mPlayers[1] = PLPlayer.GetPlayerFromName("UI");		
		if (mPlayers[1] == null)
			return false;
					
		return true;	
	}
	
	private boolean Init() {
		if(!InitSketch()) {
			PLDebug.Warn("Could Not init sketch");
			return false;
		}

		if(!InitPlayers()) {
			PLDebug.Warn("Could Not init Players");
			return false;
		}

		return true;			
	}


	private int GetNumIterations() {
		// return 1;
		 return ((PLPlayerBasicShapes)mPlayers[0]).GetNumShapes();
	}

	private boolean PlayGame(String name) {	
		PLFileUtils.MakeFullDirPath(name);
		String filename = PLFileUtils.GetQRFile(name);
		// we don't  know the file name yet
		PLFileQueryResponse file = new PLFileQueryResponse(filename);

		((PLPlayerBasicShapes)mPlayers[0]).BeginShapeQueries();
		PLShape dummy = null;
		for (int i = 0; i < GetNumIterations() ; ++i) {
			PLDebug.Warn("Shape %d", i);
			// check if forced exit
			if (mExit) break;			
									
			// clear canvas for next 
			PLGuiInfo.Obj().GetCurCanvas().ClearCanvas();

			// wait for 1 sec
			try {
				Thread.sleep(200);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			PLOutShape query = new PLOutShape();			
			float confidence = mPlayers[0].GenResponse(dummy,query);
			if (confidence < 0.3) {
				PLDebug.Warn("No more shapes");
				break;
			}

			PLSketch.Obj().DrawShape(query.GetShape(), 0, mPlayers[0].GetColor());
			
			PLOutShape response = new PLOutShape();			
			confidence = mPlayers[1].GenResponse(query.GetShape(), response);
			if (confidence > 0.3) {
				// draw the shape quickly 
				// check if necessary
				PLSketch.Obj().DrawShape(response.GetShape(), 1, mPlayers[1].GetColor());
				file.WriteQR(query.GetShape(), response.GetShape());				
			} else {
				PLDebug.Warn("Player %d Skipped Turn, confidence=%f", 1, confidence);
			}

		// wait for 1 sec
		try {
			Thread.sleep(200);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		}
		PLDebug.Warn("Finished Getting Shapes");	
		// clear canvas at last 
		PLGuiInfo.Obj().GetCurCanvas().ClearCanvas();
		
		file.FinishedWriting();

		
		return true;
	}

	private boolean Shutdown() {
		return true;
	}

	
	/*
	public static void main(String[] args) {
		PLPhaseInput game = new PLPhaseInput();

		// in a different thread
		game.Play();
		// add an exit button in a different thread
		
	}
	*/
}
